<?php


namespace App\Transformers\Product;

use App\Models\ProductReply;
use League\Fractal\TransformerAbstract;

class ProductReplyTransformer extends TransformerAbstract
{
    public $availableIncludes = ['user','product','order'];

    public function transform(ProductReply $data)
    {
        return [
            'id' => $data->id ?? '',
            'user_id' => $data->user_id ?? '',
            'order_id' => $data->order_id ?? '',
            'unique' => $data->unique ?? '',
            'product_id' => $data->product_id ?? '',
            'nickname' => $data->nickname ?? '',
            'avatar' => $data->avatar ?? '',
            'avatar_url' => 'https://shop--api.oss-cn-beijing.aliyuncs.com/shu1.png',
            'product_score' => $data->product_score ?? '',
            'service_score' => $data->service_score ?? '',
            'comment' => $data->comment ?? '',
            'pics' => $data->pics ?? '',
            'pics_url' => 'https://shop--api.oss-cn-beijing.aliyuncs.com/shu1.png',
            'merchant_reply_content' => $data->merchant_reply_content ?? '',
            'merchant_reply_time' => $data->merchant_reply_time ? $data->merchant_reply_time->format("Y-m-d H:i:s") : '',
            'is_reply' => $data->is_reply ?? 0,
            'created_at' => $data->created_at ? $data->created_at->format("Y-m-d H:i:s") : '',
        ];
    }

    /**
     * 默认返回关联的商品数据
     */
    public $defaultIncludes = ['product'];

    /**
     *额外的商品信息
     */
    public function includeProduct(ProductReply $productReply)
    {
        $product = $productReply->product;
        if (empty($product)) {
            return $this->null();
        }
        return $this->item($product,new ProductReplyNewTransformer());
    }

}
